package presentation.windows;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import java.awt.Dimension;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;


import util.Log;
import util.NotFoundException;
import util.Subsystem;
import domain.user.UnpermittedActionException;


/**
 * This is the screen for writing a new post to the forum
 */
public class AddPostFrame extends javax.swing.JFrame
{

	private static final long serialVersionUID = 7599438426143909194L ;
	private JTextArea postText;
	private JButton okPost;
	private JLabel addLable;
	private JScrollPane jScrollPane1;
	private JButton cancelPost;

	ViewThreadPanel threadPanel;

	/**
	 * Constructor
	 * @param viewThreadPanel - the thread to which the post is written
	 */
	public AddPostFrame(final ViewThreadPanel viewThreadPanel)
	{
		this.setPreferredSize(new Dimension(735, 566));
		this.threadPanel = viewThreadPanel;
		this.initGUI();
	}

	/**
	 * Initialize the addPost screen
	 */
	public void initGUI()
	{
		
		AnchorLayout thisLayout = new AnchorLayout();
		getContentPane().setLayout(thisLayout);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Adding new message");
		

		this.postText = new JTextArea();
		
		this.jScrollPane1 = new JScrollPane();
		getContentPane().add(this.jScrollPane1, new AnchorConstraint(126, 918, 846, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
		this.jScrollPane1.setPreferredSize(new java.awt.Dimension(648, 383));
		this.jScrollPane1.setViewportView(this.postText);

		this.okPost = new JButton();
		this.okPost.setText("OK");
		this.okPost.setPreferredSize(new java.awt.Dimension(73, 34));
		getContentPane().add(this.okPost, new AnchorConstraint(869, 222, 933, 121, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

		this.okPost.addActionListener(new java.awt.event.ActionListener() {
	         @SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
	        	 AddPostFrame.this.okButtonHandler();
	         }
		 });

		this.cancelPost = new JButton();
		this.cancelPost.setText("cancel");
		this.cancelPost.setPreferredSize(new java.awt.Dimension(75, 34));
		getContentPane().add(this.cancelPost, new AnchorConstraint(869, 403, 933, 300, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

		this.cancelPost.addActionListener(new java.awt.event.ActionListener() {
	         @SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
	        	 AddPostFrame.this.cancelButtonHandler();
	         }
		 });

		this.addLable = new JLabel();
		this.addLable.setText("enter your message here:");
		this.addLable.setPreferredSize(new java.awt.Dimension(207, 14));
		getContentPane().add(this.addLable, new AnchorConstraint(78, 311, 104, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

	}

	private void okButtonHandler()
	{		
		new SwingWorker<Void,Void>(){	
			@SuppressWarnings("synthetic-access")
			@Override
			protected Void doInBackground(){
				String messageText = AddPostFrame.this.postText.getText();
				if(messageText==null || messageText.equals(""))
				{
					//no message insertion:
					JOptionPane.showMessageDialog(AddPostFrame.this,
						"please enter a message", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				
				else{
		
					try
					{
						AddPostFrame.this.threadPanel.addPost(messageText);
						AddPostFrame.this.dispose(); //close the window
					}
					catch (RemoteException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to add message.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFrame.this,
							"problem with adding the message", "error occured", JOptionPane.ERROR_MESSAGE);
					}
					catch (UnpermittedActionException e)
					{
						long userId = AddPostFrame.this.threadPanel.getSharedData().getUser().getId();
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" is unpermitted to add message.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFrame.this,
							"you are unpermitted to add new messages", "error occured", JOptionPane.ERROR_MESSAGE);
					}
					catch (NotFoundException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("the wanted thread to add message to could not be found.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFrame.this,
							"problem with adding the message", "error occured", JOptionPane.ERROR_MESSAGE);
					}
				}
				return null;
			}
			@Override
			protected void done(){/*nothing to do here */}
		}.execute();
	}

	private void cancelButtonHandler()
	{
		this.dispose(); //close the window
	}
	
	/**
	 * @see java.awt.Window#dispose()
	 */
	@Override
	public void dispose()
	{
		super.dispose() ;
		this.threadPanel.removeOpenSubFrame(this);
	}


}

